<?php
	class ArcControl extends Control{
		function __auto(){
			C("debug",0);
		}

		//文章视图
		function index(){
			$db = K("arc");
			$data = $db->where("flag!=2")->all();
			$this->assign("data",$data);
			$this->display();
		}

		//添加文章视图
		function addShow(){
			//如果没有选择栏目则分配所有栏目到视图
			if(!isset($_GET['cid'])){
				$this->assignCategorydata();	
			}
			$this->display();
		}

		//添加文章
		function add(){
			$data = $_POST;
			$data['thumbimg'] = isset($data['thumbimg'][1]) ? $data['thumbimg'][1]['path'] : '';//缩略图
        	$db = M("arc");
			$data['addtime'] = time();
			$db = K("arc");
			if($db->add($data)){
				$this->success("添加文章成功！","index");
			}else{
				$this->error("添加文章失败！");
			}

		}

		//编辑文章
		function editShow(){
			if($aid = $this->_get('aid')){
				$db = M("arc");
				$arc = $db->find($aid);
				$thumbimg = array(
	                array(
	                    "alt" => "",
	                    "path" => $arc['thumbimg'],
	                    "thumb" => array($arc['thumbimg'])
	                )
            	);
           		$this->assign("thumbimg", $thumbimg);
            	$this->assignCategorydata();
            	$this->assign("data", $arc);
			}
			$this->display();
		}

		//修改文章数据
		function save(){
			if($this->_post('title')){
				$db = M("arc");
				$data = array();
				$data = $_POST;
				$data['thumbimg'] = isset($_POST['thumbimg'][1]['path']) ? $_POST['thumbimg'][1]['path'] : '';
				if($db->save($data)){
					$this->success("编辑文章成功！","index");
				}else{
					$this->error("编辑文章出错，请重试！");
				}
			}
		}

		//文章预览
		function arcShow(){
			if($aid = $this->_get('aid')){
				$arc = M("arc")->find($aid);
				$arc['addtime'] = date('Y年m月d日H:i',$arc['addtime']);
				$this->assign("data",$arc);
				$this->display();
			}
		}

		//异步删除文章
		function delArc(){
			if(isset($_POST['aid'])){
				$data = array();
				$aid = $this->_post('aid');
				$db = M('arc');
				if($result = $db->find('aid='.$aid)){
					if($result['flag']==2){
						if($db->del('aid='.$aid)){
							$data['state'] = '';
							$data['msg'] = '删除文章成功！';
						}else{
							$data['state'] = 'error';
							$data['msg'] = '删除文章失败！';
						}
					}else{
						$data = array("flag"=>2);
						if($db->where("aid=".$aid)->save($data)){
							$data['state'] = '';
							$data['msg'] = '删除文章成功！';
						}else{
							$data['state'] = 'error';
							$data['msg'] = '删除文章失败，请重试！';
						}
					}
				}else{
					$data['state'] = 'error';
					$data['msg'] = '文章不存在！';
				}
				echo json_encode($data);
			}
		}
		//获取栏目数据并分配到视图
		function assignCategorydata(){
			$db = M("category");
			$category = $db->field("cid,catname")->all();
			$this->assign("category",$category);
		}

		//文章审核
		function audit(){
			$db = K("arc");
			$data = $db->where("flag=1")->all();
			$this->assign("data",$data);
			$this->display();
		}

		//审核通过
		function aueidtok(){
			$db = M("arc");
			$aid = $_POST['aid'];
			$data = array("flag"=>0);
			if($db->where("aid=".$aid)->save($data)){
				$msg = 1;
			}else{
				$msg = 2;
			}
			echo json_encode($msg);
		}

		//文章回收站
		function trashBox(){
			$db = K("arc");
			$data = $db->where("flag=2")->all();
			$this->assign("data",$data);
			$this->display();
		}
	}
?>